package top.ruanlitao.shortlink.project.mq.redis.producer;

import cn.hutool.core.lang.UUID;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBlockingDeque;
import org.redisson.api.RDelayedQueue;
import org.redisson.api.RedissonClient;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import top.ruanlitao.shortlink.project.common.constant.RedisKeyConstant;
import top.ruanlitao.shortlink.project.dto.biz.ShortLinkStatsRecordDTO;

import java.util.concurrent.TimeUnit;

/**
 * 延迟消费短链接统计数据 发送者
 */
@ConditionalOnProperty(prefix = "short-link.message-queue", name = "type", havingValue = "redis")
@Component
@RequiredArgsConstructor
@Slf4j
@Deprecated
public class DelayShortLinkStatsProducer {

    private final RedissonClient redissonClient;

    /**
     * 发送延迟消费短链接统计信息
     *
     * @param statsRecord 短链接统计实体参数
     */
    public void send(ShortLinkStatsRecordDTO statsRecord) {
        log.info("短链接 {} 监控数据加入到延时队列", statsRecord.getFullShortUrl());
        // 设置消息的唯一标识
        statsRecord.setKeys(UUID.fastUUID().toString());
        // 获取 Redis 延迟队列
        RBlockingDeque<ShortLinkStatsRecordDTO> blockingDeque = redissonClient.getBlockingDeque(RedisKeyConstant.DELAY_QUEUE_STATS_KEY);
        RDelayedQueue<ShortLinkStatsRecordDTO> delayedQueue = redissonClient.getDelayedQueue(blockingDeque);
        // 将消息加入到队列中，延迟 5 秒钟消费
        delayedQueue.offer(statsRecord, 5, TimeUnit.SECONDS);
    }
}
